package com.sinoglobal.sinostore.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.StateBaseActivity;
import com.sinoglobal.sinostore.bean.CityVo;
import com.sinoglobal.sinostore.fragment.BaseFragment.AddressInterface;
import com.sinoglobal.sinostore.fragment.CityFragment;
import com.sinoglobal.sinostore.fragment.ProvinceFragment;
import com.sinoglobal.sinostore.fragment.ZoneFragment;
import com.sinoglobal.sinostore.utils.SQLiteManager;

/**
 * @author lk
 * @createdate 2014-9-10 下午2:15:26
 * @Description: TODO(选择城市地址)
 */
public class SelAddressActivity extends StateBaseActivity implements
        AddressInterface, OnClickListener {
    private final String FRAGMENTTAG = "FRAGMENTTAG";// fragment添加标签
    private FragmentManager manager;
    private TextView tvProvince;// 省
    private TextView tvCity;// 市
    private String cityId;// 标记城市的id
    private SQLiteManager sqLiteManager;
    private RelativeLayout llSelCity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        titleView.setText("添加收货地址");
        templateButtonRight.setVisibility(View.INVISIBLE);
        templateButtonLeft.setOnClickListener(this);
        setContentView(R.layout.shop_city_layout);
        openChenJInShi();
        // 初始化数据
        initView();
        //实例化数据库管理对象，Fragment管理对象
        sqLiteManager = new SQLiteManager(this);
        manager = getSupportFragmentManager();
        //初始化界面显示
        getData();
    }

    /**
     * @author lk
     * @createdate 2014-9-11 下午6:34:08
     * @Description: ()
     */
    private void getData() {
        Intent intent = getIntent();
        String str_address = intent.getStringExtra("address");
        // 有地址时从市级往上展示
        if (null != str_address) {
            // 将地址按省市区三级分割字符串 存放在数组中
            String[] addresss = str_address.split("-");
            // 根据市的名称查询出该市的Id以及省的Id
            CityVo cityVo = sqLiteManager.getCityByName(addresss[1]);
            // 在添加地址的上方显示省市
            llSelCity.setVisibility(View.VISIBLE);
            tvProvince.setText(addresss[0]);
            tvCity.setText(addresss[1]);
            tvCity.setVisibility(View.VISIBLE);
            // 加载选中省市的区级数据，在Fragment中加载
            ZoneFragment fragment = new ZoneFragment();
            Bundle bundle = new Bundle();
            // 将市的对象传到Fragment中
            bundle.putSerializable("city", cityVo);
            fragment.setArguments(bundle);
            jumpFragment(fragment);
            cityId = cityVo.getLastCityId();
        } else {
            // 添加新地址时从省级往下展示
            ProvinceFragment fragment = new ProvinceFragment();
            jumpFragment(fragment);
        }
    }

    /**
     * @author lk
     * @createdate 2014-9-11 上午11:58:41
     * @Description: (初始化数据)
     */
    private void initView() {
        tvProvince = (TextView) this.findViewById(R.id.province);
        tvCity = (TextView) this.findViewById(R.id.city);
        llSelCity = (RelativeLayout) this.findViewById(R.id.ll_sel_city);

        tvProvince.setOnClickListener(this);
        tvCity.setOnClickListener(this);
    }

    @Override
    public void setCity(CityVo cityVo, Fragment fragment, Fragment fragment2) {
        if (null != fragment && null != fragment2) {
            // 判断当前的Fragment是否是省级的Fragment 如果是省级的 将选中的省份的名称显示在Activity的头部
            if (fragment instanceof ProvinceFragment) {
                llSelCity.setVisibility(View.VISIBLE);
                tvProvince.setText(cityVo.getCityName());
                this.cityId = cityVo.getCityId();
            }
            // 判断当前的Fragment是否是市级的Fragment 如果是市级的 将选中的市的名称显示在Activity的头部
            if (fragment instanceof CityFragment) {
                tvCity.setVisibility(View.VISIBLE);
                tvCity.setText(cityVo.getCityName());
            }
            // 跳转Fragment
            jumpFragment(fragment2);
        } else {
            // 传递的Fragment都为空时 说明选中的区级了
            String pro = tvProvince.getText().toString();
            String city = tvCity.getText().toString();
            // 将取得省市区用-隔开，方便后面操作
            StringBuffer sb = new StringBuffer(pro);
            sb.append("-").append(city);
            if (null != cityVo) {
                sb.append("-").append(cityVo.getCityName());
            }
            Intent intent = new Intent(this, AddAddressActivity.class);
            intent.putExtra("address", sb.toString());
            startActivity(intent);
            this.finish();
            overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
        }
    }

    /**
     * @author lk
     * @createdate 2014-9-11 下午5:03:37
     * @Description: (省级Fragment跳转)
     */
    private void provinceFragment() {
        tvCity.setVisibility(View.GONE);
        llSelCity.setVisibility(View.GONE);
        ProvinceFragment pFragment = new ProvinceFragment();
        jumpFragment(pFragment);
    }

    /**
     * @author lk
     * @createdate 2014-9-11 下午5:04:31
     * @Description: (市级Fragment跳转)
     */
    private void cityFragment() {
        tvCity.setVisibility(View.GONE);
        CityFragment cityFragment = new CityFragment();
        Bundle bundle = new Bundle();
        bundle.putString("province", this.cityId);
        cityFragment.setArguments(bundle);
        jumpFragment(cityFragment);
    }

    /**
     * @param fragment 要跳转的Fragment
     * @author lk
     * @createdate 2014-9-11 下午4:55:16
     * @Description: (Fragment跳转)
     */
    private void jumpFragment(Fragment fragment) {
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.city_layout, fragment, FRAGMENTTAG);
        transaction.setCustomAnimations(R.anim.push_left_in, 0, 0,
                R.anim.push_left_out);
        transaction.commit();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.province) {
            // 跳转到省级
            provinceFragment();
        } else if (id == R.id.city) {
            // 跳转市级
            cityFragment();
        } else if (id == R.id.shop_title_but_left) {
            back();// 点击头部返回按钮实现Fragment跳转
        } else {
            this.finish();
        }
    }

    /**
     * @author lk
     * @createdate 2014-9-11 下午5:24:38
     * @Description: (点击按钮跳转方法)
     */
    private void back() {
        // 获取当前activity中显示的Fragment
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(
                FRAGMENTTAG);
        // 判断是否是区级Fragment
        if (fragment instanceof ZoneFragment) {
            // 跳转市级
            cityFragment();
        } else if (fragment instanceof CityFragment) {// 判断是否是市级Fragment
            // 跳转到省级
            provinceFragment();
        } else {
            // 如果是省级Fragment点击关闭当前Activity
            this.finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        back();
        return false;
    }
}
